package com.acme.bankapp.domain.bank;


public class Bank {
	
	private Client[] clients;
	private ClientRegistrationListener [] listeners;
	
	public Bank() {
		
		class PrintClientListener implements ClientRegistrationListener {
			public void onClientAdded(Client c) {
				System.out.println("New client "+c.getClientSalutation()+": "+c.getAccount().maximumAmountToWithdraw());
			}		
		}
		
		class EmailNotificationListener implements ClientRegistrationListener {
			public void onClientAdded(Client c) {
				System.out.println("Notification email for client "+c.getClientSalutation()+" to be sent");
			}		
		}
		
		clients = new Client[10];
		listeners = new ClientRegistrationListener[2];
		listeners[0] = new PrintClientListener();
		listeners[1] = new EmailNotificationListener();
	}
	
	public void addClient(Client client)
	{
		for (int i = 0; i < clients.length; i++)
		{
			if (clients[i] == null)
			{
				clients[i] = client;
				for (ClientRegistrationListener listener: listeners) {
					listener.onClientAdded(client);
				}
				return;
			}
		}
		// Exception: array is full
	}
	
	public Client getClient(int num)
	{
		return clients[num];
	}
	
	public int numClients() {
		for (int i = 0; i < clients.length; i++)
		{
			if (clients[i] == null)
			{
				return i;
			}
		} 
		return clients.length;
	}
	
}
